<?php


namespace App\Http\Controllers\Api\Repositories;

use App\Jobs\AttachIpAddressJob;
use App\Jobs\TopicImgJob;
use App\Models\Posts\WxTag;
use App\Wen\Utils\RealtimeUtils;
use App\Wen\Utils\Settings;
use App\Models\Posts\WxPostsTag;
use App\Models\Posts\WxTag as Model;
use Illuminate\Support\Facades\DB;

class WxTagsRepositories
{
    protected $eloquentClass = Model::class;

    public static function add($uid, $tags_name, $tags_state = 0)
    {
        $tags_name = trim($tags_name);

        $tag = WxTag::withTrashed()->where('tags_name', $tags_name)->first();
        if($tag){
            if ($tag->trashed()) {
                $tag->restore();
            }
            return $tag;
        }

        $tagModel = new Model();
        $tagModel->user_id = $uid;
        $tagModel->tags_name = strip_tags($tags_name);
        $tagModel->tags_number = 0;
        $tagModel->tags_state = $tags_state;

        global $__MINI_GLOBAL_IP__;
        if($__MINI_GLOBAL_IP__){
            $tagModel->ip = $__MINI_GLOBAL_IP__;
        }

        $r = $tagModel->save();
        if($r){
            if($__MINI_GLOBAL_IP__){
                AttachIpAddressJob::dispatch(5, $tagModel->id)->delay(2);
            }
        }
        return $tagModel;
    }

    /**
     * 发帖时推荐的话题
     */
    public static function recommendTags()
    {
        $create_tags = explode(',', Settings::get('app_page_create_tags', '', true));
        if($create_tags && $create_tags[0]){
            return (new Model())::whereIn('id', $create_tags)->orderBy(DB::raw('FIND_IN_SET(id, "' . implode(",", $create_tags) . '"' . ")"))->get(['id','tags_name', 'head_portrait','tags_state'])->map(function ($v){
                $v->tags_number = WxPostsTag::where('tags_id', $v->id)->count();
                if(_empty_($v->head_portrait)){
                    $v->head_portrait = Settings::get('img_default_topic_avatar');
                    TopicImgJob::dispatch($v->id);
                }
                return $v;
            });
        }else{
            global $__MINI_GLOBAL_PAYCODE_12__;
            if($__MINI_GLOBAL_PAYCODE_12__){
                $topics = RealtimeUtils::get_topic_top();
                if($topics['topics']){
                    return array_slice($topics['topics'], 0, 20);
                }else{
                    return null;
                }
            }else{
                $tags = (new Model())
                    ->where('tags_state', 0)
                    ->where('tags_number', '>', 0)
                    ->orderBy('tags_number', 'desc')->limit(20)->get();
                $tags->map(function ($items) {
                    $items->tags_number = WxPostsTag::where('tags_id', $items->id)->count();
                    if(_empty_($items->head_portrait)){
                        $items->head_portrait = Settings::get('img_default_topic_avatar');
                        TopicImgJob::dispatch($items->id);
                    }
                });
                return $tags;
            }
        }
    }

    public static function hot_tags($scene, $files = []){
        $data = null;
        if($scene){
            if($scene == 10004){
                if(Settings::get('app_hot_tags', '')){
                    $data =  Model::wherein('id', explode(',', Settings::get('app_hot_tags', '')))->orderBy('sort', 'desc')->limit(10);
                }
            }else if($scene == 39){
                if(Settings::get('app_page_hot_tags', '')){
                    $data =  Model::wherein('id', explode(',', Settings::get('app_hot_tags', '')))->orderBy('sort', 'desc')->limit(10);
                }
            }
        }
        if(_empty_($data)){
            $data = Model::where('tags_state', 0)
                ->orderBy('tags_number', 'desc')->limit(10);
        }
        if($files){
            return $data->get($files);
        }else{
            return $data->get();
        }
    }



}
